package _base0to1;

/**
 * 13. 罗马数字转整数
 */
public class No13 {
    public int romanToInt(String s) {
        int answer = 0;
        int pre = 0;
        for (int i = s.length() - 1; i >= 0; i--) {
            int value = switch (s.charAt(i)) {
                case 'I' -> 1;
                case 'V' -> 5;
                case 'X' -> 10;
                case 'L' -> 50;
                case 'C' -> 100;
                case 'D' -> 500;
                case 'M' -> 1000;
                default -> 0;
            };
            if (pre > value) answer -= value;
            else answer += value;
            pre = value;
        }
        return answer;
    }
}
